package com.bub.pay.core.manager.user.async;

import com.bub.pay.core.dao.SysLoginLogDao;
import com.bub.pay.core.dao.SysUserDao;
import com.bub.pay.core.entity.domain.SysLoginLog;
import com.bub.pay.core.entity.vo.user.LoginVo;
import com.bub.pay.core.enums.SysLoginTypeEnum;
import com.bub.pay.core.manager.system.CurrentSystemManager;
import com.bub.pay.framework.common.entity.model.SessionModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class AsyncLoginLogRunnable implements Runnable {

    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger(AsyncLoginLogRunnable.class);

    private final LoginVo loginVo;
    private final SysUserDao sysUserDao;
    private final SysLoginLogDao sysLoginLogDao;
    private final CurrentSystemManager currentSystemManager;
    private SessionModel sessionModel;
    private Date loginTime;

    public AsyncLoginLogRunnable(final LoginVo loginVo, final SysUserDao sysUserDao, final SysLoginLogDao sysLoginLogDao, final CurrentSystemManager currentSystemManager, SessionModel sessionModel) {
        this.loginVo = loginVo;
        this.sysUserDao = sysUserDao;
        this.sysLoginLogDao = sysLoginLogDao;
        this.currentSystemManager = currentSystemManager;
        this.sessionModel = sessionModel;
        this.loginTime = new Date();
    }

    private boolean isEmptyParam() {
        return null == loginVo || null == sysUserDao || null == sysLoginLogDao;
    }

    @Override
    public void run() {
        if (isEmptyParam()) {
            log.error("AsyncLoginLogRunnable parameter must not be null");
            return;
        }
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setCreateId(sessionModel.getUserId());
        sysLoginLog.setCreateName(sessionModel.getLoginName());
        sysLoginLog.setCreateDate(loginTime);
        sysLoginLog.setUserType(sessionModel.getUserType());
        sysLoginLog.setIp(sessionModel.getLoginIp());
        sysLoginLog.setDelFlag(false);
        sysLoginLog.setType(SysLoginTypeEnum.login.getType());
        sysLoginLog.setSystemId(currentSystemManager.getModule().getSn());
        sysLoginLog.setSystemName(currentSystemManager.getModule().getDesc());
        sysLoginLogDao.insert(sysLoginLog);
    }

}
